﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.RequestSecurityToken
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using Microsoft.InfoCards.Diagnostics;
using System;
using System.Globalization;
using System.ServiceModel.Channels;
using System.ServiceModel.Security.Tokens;
using System.Xml;

namespace Microsoft.InfoCards
{
  internal abstract class RequestSecurityToken : BodyWriter
  {
    protected RequestSecurityTokenParameters m_rstParams;
    protected RequestSecurityTokenSerializer m_serializer;
    protected XmlDictionaryWriter m_xmlWriter;

    protected ProtocolProfile ProtocolVersionProfile
    {
      get
      {
        return this.m_rstParams.Profile;
      }
    }

    protected InfoCardPolicy Policy
    {
      get
      {
        return this.m_rstParams.Policy;
      }
    }

    protected RequestSecurityTokenSerializer Serializer
    {
      get
      {
        return this.m_serializer;
      }
      set
      {
        this.m_serializer = value;
      }
    }

    protected XmlDictionaryWriter Writer
    {
      get
      {
        return this.m_xmlWriter;
      }
      set
      {
        this.m_xmlWriter = value;
      }
    }

    protected string WstPrefix
    {
      get
      {
        return this.ProtocolVersionProfile.WSTrust.DefaultPrefix;
      }
    }

    public RequestSecurityToken(RequestSecurityTokenParameters rstParams)
      : base(false)
    {
      this.m_rstParams = rstParams;
    }

    protected virtual void WriteRSTOpeningElement()
    {
      this.Writer.WriteStartElement(this.WstPrefix, this.ProtocolVersionProfile.WSTrust.RequestSecurityToken, this.ProtocolVersionProfile.WSTrust.Namespace);
      this.Writer.WriteAttributeString(this.ProtocolVersionProfile.WSTrust.Context, (string) null, this.m_rstParams.Context);
    }

    protected virtual void WriteInfoCardReferenceElement()
    {
      if (this.m_rstParams.Card == null)
        return;
      this.Serializer.WriteInfoCardReferenceElement(this.m_rstParams.Card);
    }

    protected virtual void WriteKeyTypeElement()
    {
      this.Serializer.WriteKeyTypeElement(this.Policy.GetKeyTypeString());
    }

    protected virtual void WriteKeySupportingElements()
    {
      if (this.Policy.KeyType == SecurityKeyTypeInternal.SymmetricKey)
      {
        this.Serializer.WriteKeySizeElement(this.Policy.GetIntelligentKeySize(false).ToString((IFormatProvider) CultureInfo.InvariantCulture));
        InfoCardTrace.Assert(null != this.m_rstParams.ClientEntropyForSymmetric, "Should not be null");
        this.Serializer.WriteBinarySecretElement(new BinarySecretSecurityToken(this.m_rstParams.ClientEntropyForSymmetric));
        if (!string.IsNullOrEmpty(this.Policy.OptionalRstParams.EncryptWith))
          this.Serializer.WriteEncryptWithElement(this.Policy.OptionalRstParams.EncryptWith);
        if (string.IsNullOrEmpty(this.Policy.OptionalRstParams.SignWith))
          return;
        this.Serializer.WriteSignWithElement(this.Policy.OptionalRstParams.SignWith);
      }
      else if (SecurityKeyTypeInternal.AsymmetricKey == this.Policy.KeyType)
      {
        if (XmlNames.WSSpecificationVersion.WSTrustOasis2007 == this.ProtocolVersionProfile.WSTrust.Version && !string.IsNullOrEmpty(this.Policy.OptionalRstParams.KeyWrapAlgorithm))
          this.Serializer.WriteKeyWrapAlgorithmElement(this.Policy.OptionalRstParams.KeyWrapAlgorithm);
        this.Serializer.WriteKeySizeElement(this.Policy.GetIntelligentKeySize(false).ToString((IFormatProvider) CultureInfo.InvariantCulture));
        InfoCardTrace.Assert(null != this.m_rstParams.RSAKey, "Should have been populated in asymmetric case");
        this.Serializer.WriteUseKeyElement(this.m_rstParams.RSAKey.GetXml());
        if (!string.IsNullOrEmpty(this.Policy.OptionalRstParams.EncryptWith))
          this.Serializer.WriteEncryptWithElement(this.Policy.OptionalRstParams.EncryptWith);
        if (string.IsNullOrEmpty(this.Policy.OptionalRstParams.SignWith))
          return;
        this.Serializer.WriteSignWithElement(this.Policy.OptionalRstParams.SignWith);
      }
      else
        InfoCardTrace.Assert(SecurityKeyTypeInternal.NoKey == this.Policy.KeyType, "Should be no proof key");
    }

    protected virtual void WriteRequestTypeElement()
    {
      if (!string.IsNullOrEmpty(this.Policy.RequestType))
        this.Serializer.WriteRequestTypeElement(this.Policy.RequestType);
      else
        this.Serializer.WriteRequestTypeElement(this.ProtocolVersionProfile.WSTrust.IssueRequestType);
    }

    protected virtual void WritePPIDElement()
    {
      if (this.m_rstParams.Card == null || !this.m_rstParams.Card.GetClaims().ContainsKey(InfoCardConstants.PPIDClaimsUri))
        return;
      this.Serializer.WritePPIDElement(Utility.CreateHash(this.m_rstParams.Card.Key, Convert.FromBase64String(this.Policy.ImmediateTokenRecipient.GetOrganizationPPIDIdentifier()), this.m_rstParams.Card.HashSalt));
    }

    protected virtual void WriteEncryptionAlgorithmElement()
    {
      if (string.IsNullOrEmpty(this.Policy.OptionalRstParams.EncryptionAlgorithm))
        return;
      this.Serializer.WriteEncryptionAlgorithmElement(this.Policy.OptionalRstParams.EncryptionAlgorithm);
    }

    protected virtual void WriteClaimsElement()
    {
      this.Serializer.WriteClaimsElement(this.m_rstParams.DisclosedClaims, this.Policy.RequiredClaims, this.Policy.OptionalClaims);
    }

    protected virtual void WriteDisplayTokenElement()
    {
      if (this.m_rstParams.Culture == null)
        return;
      this.Serializer.WriteDisplayTokenElement(this.m_rstParams.Culture);
    }

    protected virtual void WriteTokenTypeElement()
    {
      if (string.IsNullOrEmpty(this.Policy.OptionalRstParams.TokenType))
        return;
      this.Serializer.WriteTokenTypeElement(this.Policy.OptionalRstParams.TokenType);
    }

    protected virtual void WritePassOnElements()
    {
      this.Policy.OptionalRstParams.WritePassOnElements(this.Writer, this.ProtocolVersionProfile);
    }

    protected virtual void WriteUnprocessedPolicyElements()
    {
      if (this.Policy.UnprocessedPolicyElements == null || this.Policy.UnprocessedPolicyElements.Length <= 0L)
        return;
      this.Serializer.WriteUnprocessedPolicyElements(this.Policy.UnprocessedPolicyElements);
    }

    protected abstract void WriteSecondaryParametersElement();

    protected virtual void WriteEndElement()
    {
      this.Writer.WriteEndElement();
    }

    protected abstract void WriteAppliesToElement();

    protected virtual void InitializeWriters(XmlDictionaryWriter writer)
    {
      this.Writer = writer;
      this.Serializer = new RequestSecurityTokenSerializer((XmlWriter) writer, this.ProtocolVersionProfile);
    }

    protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
    {
      this.CustomWriteBodyContents(writer);
    }

    protected abstract void CustomWriteBodyContents(XmlDictionaryWriter writer);
  }
}
